<?php

/*
 * Copyright 2008 the original author or authors.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#namespace tehframework\objects\propertyeditors;

require_once 'test/testHelper.php';

class ArrayEditorTest extends BaseEditorTestCase
{
	/**
	 * @return ArrayEditor
	 */
	protected function createEditor()
	{
		return new ArrayEditor();
	}
	
	function testCanEditPropertyReturnsTrueForAnyType()
	{
		$h = fopen(__FILE__, 'r');
		
		try
		{
			$this->assertTrue($this->e->canEditProperty($h));
		}
		catch (Exception $x)
		{
			fclose($h);
			
			throw $x;
		}
		
		$propertyValues = array(
			1, 1.1, 'foo', new stdClass, $this, true, false, null, array(1, 'two'));
		
		foreach ($propertyValues as $propertyValue)
		{
			$this->assertTrue($this->e->canEditProperty($propertyValue));
		}
	}
	
	function testEditPropertyReturnsEmptyArrayIfSpecifiedValueIsNotArrayNorObject()
	{
		$h = fopen(__FILE__, 'r');
	
		try
		{
			$this->assertSame(array(), $this->e->editProperty($h));
		}
		catch (Exception $x)
		{
			fclose($h);
			
			throw $x;
		}
		
		$propertyValues = array('', 'foo', '1', 1, 0, -1.2, null, false, true);
		
		foreach ($propertyValues as $propertyValue)
		{
			$this->assertSame(array(), $this->e->editProperty($propertyValue));
		}
	}
	
	function testEditPropertyReturnsSpecifiedValueIfItsTypeIsArray()
	{
		$propertyValue = array(1, 'two', 3);
		$this->assertSame($propertyValue, $this->e->editProperty($propertyValue));
		
		$propertyValue = array();
		$this->assertSame($propertyValue, $this->e->editProperty($propertyValue));
	}
	
	function testEditPropertyReturnsArrayConstructedFromIterationThroughObject()
	{
		$o = new stdClass;
		$o->p1 = 1;
		$o->p2 = 'two';
		$o->p3 = true;
		
		$expected = array();
		
		foreach ($o as $k => $v)
		{
			$expected[$k] = $v;
		}
		
		$this->assertSame($expected, $this->e->editProperty($o));
	}
	
	function testEditPropertyReturnsArrayConstructedFromIterationThroughObject2()
	{
		$o = new ArrayObject();
		$o['p1'] = 1;
		$o['p2'] = 'two';
		$o['p3'] = true;
		
		$expected = array();
		
		foreach ($o as $k => $v)
		{
			$expected[$k] = $v;
		}
		
		$this->assertSame($expected, $this->e->editProperty($o));
	}
}